package com.first.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.first.config.redis.jedis.IRedisUtil;
import com.first.config.redis.jedis.SpringJedisUtilInterface;
import com.first.config.redis.lock.LockMethod;
import com.first.config.redis.lock.LockParameter;
import com.first.springBoot.domain.BusUser;
import com.first.springBoot.service.BusUserService;

@RestController
public class HolloSpringBoot {
   @Autowired
   private BusUserService BusUserService;
   
   @Autowired
   private IRedisUtil redisUtil;
   @Autowired
   private SpringJedisUtilInterface sJedisUtil;
   
    @RequestMapping("/hello")
    @LockMethod(preKey="sas")
    public String index(HttpServletRequest req,HttpSession session,@LockParameter int pageNum, int pageSize) {
    	redisUtil.set("redisUtil","dhasjkgh");
    	sJedisUtil.set("sJedisUtil", "safdsfds");
    	 String a = "redisUtil:"+redisUtil.get("redisUtil");
    	 a = a+"----sJedisUtil: "+redisUtil.get("sJedisUtil");
    	 req.getSession().setAttribute("dasdas", "dasdasda");
    	session.setAttribute("kaka", "dsad");
        return a;
    }
    
    @RequestMapping("/sessiontest")
    public String sessionTest(HttpSession session) {
    	Object a = session.getAttribute("kaka");
    	Object b = session.getAttribute("dasdas");
    	BusUser busUser= BusUserService.selectByPrimaryKey(257L);
        return session.getId()+"----"+a+"----"+b+"---BusUser:"+busUser;
    }
    
    
    @RequestMapping("/listuser/{pageNum}/{pageSize}")
    public List<BusUser> listuser(@PathVariable("pageNum") int pageNum
    		,@PathVariable("pageSize") int pageSize) {
    	Map<String, Object> map = new HashMap<>();
    	map.put("pageNum", pageNum);
    	map.put("pageSize", pageSize);
    	
        return BusUserService.selectByCondition(map);
    }
    
    @RequestMapping("/update/{userId}/{userName}")
    public String updateUser(@PathVariable("userId") long userId
    		,@PathVariable("userName") String userName) {
    	BusUser record = new BusUser();
    	record.setId(userId);
    	record.setUsername(userName);
    	int cont = BusUserService.updateByPrimaryKeySelective(record);
    	System.out.println(cont);
        return "修改成功的条数："+cont;
    }

}
